package com.neusoft.lohas.common.utils;

import java.awt.image.BufferedImage;
import java.awt.image.ImagingOpException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;

/**
 * 图片处理类，处理图片压缩，格式转换等问题
 * @author jackie.liu
 *
 */
public class ImageHandleUtill {
	public static BufferedImage createCellThumbnail(BufferedImage image) {
		BufferedImage thumbnail =
				Scalr.resize(image, Scalr.Method.SPEED,
						Scalr.Mode.FIT_TO_WIDTH,
						360, 180, Scalr.OP_ANTIALIAS);
		return thumbnail;
	}

	public static BufferedImage createLogoThumbnail(BufferedImage image,
			int targetWidth, int targetHeight) {
		try {
			return Scalr.resize(image, Scalr.Method.SPEED,
					Scalr.Mode.AUTOMATIC,
					targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ImagingOpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static BufferedImage createCellThumbnail(InputStream image) {

		try {
			return createCellThumbnail(ImageIO.read(image));
		} catch (IOException e) {
			e.printStackTrace();
			return null;

		}
	}

	public static void main(String[] args) {
		try {
			long timeStart = System.currentTimeMillis();
			System.out.println(timeStart);
			for (int i = 0; i <= 10; i++) {
				BufferedImage bufferedImage =
						ImageIO.read(new FileInputStream(new File(
								"D:/image/client/1.png")));
				BufferedImage thumbnail = createCellThumbnail(bufferedImage);
				ImageIO.write(
						thumbnail,
						"jpeg",
						new File(
								"D:/image/client/thumbnail/1_"
										+ System.currentTimeMillis()
										+ ".jpg"));

			}
			long endTime = System.currentTimeMillis();
			System.out.println(endTime);
			System.out.println((timeStart - endTime) / 1000.0);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
